<template>
  <div>
    <el-form label-width="80px" :model="userdata" :rules="rules" ref="userfrom">
      <el-form-item label="用户名">
        <el-input v-model="userInfo.userName" disabled></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="userInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="userEmail">
        <el-input v-model="userInfo.userEmail"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input v-model="userInfo.userPhone"></el-input>
      </el-form-item>
      <el-form-item label="个性签名" prop="userSign">
        <el-input type="textarea" v-model="userInfo.userSign"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "UserInfo",
  data() {
    return {
      userInfo: {
        userName: "",
        name: "",
        userEmail: "",
        userPhone: "",
        userSign: "",
      },

      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        userSign: [{ required: true, message: "不能为空", trigger: "blur" }],
        userEmail: [{ required: true, message: "不能为空", trigger: "blur" }],
        userPhone: [
          { required: true, message: "电话信息", trigger: "blur" },
          { min: 11, max: 11, message: "电话格式不对", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    userdata() {
      //从vuex获取数据
      var data = this.$store.getters.getUserdata;
      //获取数据
      console.log("获取到数据");
      console.log(data);
      this.userInfo.name = data.name;
      this.userInfo.userName = data.userName;
      this.userInfo.userEmail = data.userEmail;
      this.userInfo.userPhone = data.userPhone;
      this.userInfo.userSign = data.userSign;

      return this.userInfo;
    },
  },

  created() {},

  methods: {
    submitForm() {
      this.$refs["userfrom"].validate((valid) => {
        if (valid) {
          this.$store.dispatch("updateUserinfo", {
            name: this.userInfo.name,
            userSign: this.userInfo.userSign,
            userEmail: this.userInfo.userEmail,
            userPhone: this.userInfo.userPhone,
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style>
</style>